﻿using DG.Tweening;
using ScFramework.Core;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MeshComp : MonoBehaviour
{
    public TriData Data = null;
    public float RedTime = 3f;
    Specials sp;
    public Specials SP
    {
        get => sp;
        set
        {
            sp = value;
            GetComponentInChildren<SpriteRenderer>().sprite = ResourceManager.Instance.Load<Sprite>("Sprite/"+SP.ToString());
            transform.GetChild(0).localScale = Mathf.Sqrt(UnsafeTool.GetArea(Data.Points.ToArray())) / 30f * Vector3.one;
            var pos =(Vector3) Data.Center;
            pos.z = -20;
            transform.GetChild(0).localPosition = pos;
        }
    }
    bool canDestory = true;
    private void OnEnable()
    {
        
    }
    public void Enter(TriEnum enterType)
    {
        OnDestory = false;
        if (Data.Type == TriEnum.Red)
        {
            if (InstanceManager.Instance.MapDic.ContainsKey(Data)&&canDestory)
            {
                transform.DOShakePosition(RedTime,Mathf.Sqrt(UnsafeTool.GetArea(Data.Points.ToArray()))/10f,fadeOut:false);
                Main.Instance.StartCoroutine(Wait());

            }
        }
        if (SP == Specials.Key)
        {
            InstanceManager.Instance.KeyCnt--;
            SP = Specials.None;
        }
        else if (SP == Specials.Final && InstanceManager.Instance.KeyCnt == 0)
        {
            InstanceManager.Instance.NextLevel();   
        }
    }
   IEnumerator Wait()
    {
        canDestory = false;
        yield return new WaitForSeconds(RedTime);
        
        InstanceManager.Instance.Destory(Data);
        canDestory = true;
    }
    bool OnDestory = false;
    public void Exit()
    {
        if (Data.Type == TriEnum.Yellow)
        {
            OnDestory = true;
            //InstanceManager.Instance.ChangeType(Data,TriEnum.Orange);
        }else if (Data.Type == TriEnum.Blue)
        {
            InstanceManager.Instance.Destory(Data);
        }
    }
    public TriData Tri
    {
        get
        {
            return Data;
        }
        set
        {
            Data = value;
            if (Main.Instance != null)
            {

              GetComponent<MeshRenderer>().material = Main.Instance.Config.MatDic.Find(s => s.Type == value.Type).Mat;
            }
            else
            {
                GetComponent<MeshRenderer>().material = Resources.Load<Conf>("Config/Conf").MatDic.Find(s => s.Type == value.Type).Mat;
            }
        }
    }
    private void Update()
    {
        if (OnDestory && Data.Type == TriEnum.Yellow)
        {

            var list =new List<Collider2D>();
             GetComponent<PolygonCollider2D>().OverlapCollider(new ContactFilter2D(), list);
            list.RemoveAll(s => s.name.StartsWith("Ball") == false);
            Debug.Log("yellow collide with "+list.Count);
            if (list.Count <= 10)
            {
                InstanceManager.Instance.ChangeType(Data, TriEnum.Orange);
                OnDestory = false;
            }
        }
    }
}
